local n $n
local dataspec $dataspec

use "$working/$dataspec", clear

merge m:1 year using "$working/wholesale_priceindex"
drop if _merge==2
drop _merge

*Instead of 100, make base year 1
replace wholesale_priceindex = wholesale_priceindex/100

generate sales_b2010 = sales_value/wholesale_priceindex

xtset  co_code1 year, yearly
destring(nic_08_2dig), g(nic2)

gen diff_ln_IND = ln_IND - L.ln_IND
gen diff_ln_IVS = ln_IVS - L.ln_IVS
gen diff_tfpr = tfpr - L.tfpr

foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate ln_`var' = ln(`var')
	generate diff_`var' = ln_`var' - L.ln_`var'
}

foreach v in diff_tfpr diff_ln_IND diff_ln_IVS {
	drop if missing(`v')
}

bys co_code1: egen mean_firm_sales = mean(sales_b2010)
egen size = xtile(mean_firm_sales), n(2) by(nic2)

** To get the final sample for all regs:
	ivreghdfe drop_term ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2#year) first cluster(nic_08_4dig)
			generate sample0 = e(sample)
			keep if sample0 == 1

foreach var in diff_tfpr dif_d_bar dif_a_bar dif_cov_stay dif_var_adj adding_term drop_term {
preserve
* Baseline IV 		
	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year) first cluster(nic_08_4dig)
				local b`n'size1_y = _b[1b.size#c.diff_ln_IND]
				local b`n'size2_y = _b[2.size#c.diff_ln_IND]
				local se_b`n'size1_y = _se[1b.size#c.diff_ln_IND]
				local se_b`n'size2_y = _se[2.size#c.diff_ln_IND]
				cap local I_b`n'size1_y = _b[1b.size#c.diff_wIPT_IND_CHN]
				cap local I_b`n'size2_y = _b[2.size#diff_wIPT_IND_CHN]
				cap local se_I_b`n'size1_y = _se[1b.size#c.diff_wIPT_IND_CHN]
				cap local se_I_b`n'size2_y = _se[2.size#diff_wIPT_IND_CHN]
				local N`n'_y = e(N)
				mat f = e(first)
				local F_b`n'size1_y = round(f[8,1], .01)
				local F_b`n'size2_y = round(f[8,2], .01)
				cap local F_I_b`n'size1_y = round(f[8,3], .01)
				cap local F_I_b`n'size2_y = round(f[8,4], .01)
				

	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year nic2) first cluster(nic_08_4dig)
				local b`n'size1_yn = _b[1b.size#c.diff_ln_IND]
				local b`n'size2_yn = _b[2.size#c.diff_ln_IND]
				local se_b`n'size1_yn = _se[1b.size#c.diff_ln_IND]
				local se_b`n'size2_yn = _se[2.size#c.diff_ln_IND]
				cap local I_b`n'size1_yn = _b[1b.size#c.diff_wIPT_IND_CHN]
				cap local I_b`n'size2_yn = _b[2.size#c.diff_wIPT_IND_CHN]
				cap local se_I_b`n'size1_yn = _se[1b.size#c.diff_wIPT_IND_CHN]
				cap local se_I_b`n'size2_yn = _se[2.size#c.diff_wIPT_IND_CHN]
				local N`n'_yn = e(N)
				mat f = e(first)
				local F_b`n'size1_yn = round(f[8,1], .01)
				local F_b`n'size2_yn = round(f[8,2], .01)
				cap local F_I_b`n'size1_yn = round(f[8,3], .01)
				cap local F_I_b`n'size2_yn = round(f[8,4], .01)
				
	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(nic2#year) first cluster(nic_08_4dig)
				local b`n'size1_ycn = _b[1b.size#c.diff_ln_IND]
				local b`n'size2_ycn = _b[2.size#c.diff_ln_IND]
				local se_b`n'size1_ycn = _se[1b.size#c.diff_ln_IND]
				local se_b`n'size2_ycn = _se[2.size#c.diff_ln_IND]
				cap local I_b`n'size1_ycn = _b[1b.size#c.diff_wIPT_IND_CHN]
				cap local I_b`n'size2_ycn = _b[2.size#c.diff_wIPT_IND_CHN]
				cap local se_I_b`n'size1_ycn = _se[1b.size#c.diff_wIPT_IND_CHN]
				cap local se_I_b`n'size2_ycn = _se[2.size#c.diff_wIPT_IND_CHN]
				local N`n'_ycn = e(N)
				mat f = e(first)
				local F_b`n'size1_ycn = round(f[8,1], .01)
				local F_b`n'size2_ycn = round(f[8,2], .01)
				cap local F_I_b`n'size1_ycn = round(f[8,3], .01)
				cap local F_I_b`n'size2_ycn = round(f[8,4], .01)
				
* Baseline OLS
	* Year FE
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(year)  cluster(nic_08_4dig)
				local Ob`n'size1_y = _b[1b.size#c.diff_ln_IND]
				local Ob`n'size2_y = _b[2.size#c.diff_ln_IND]
				local se_Ob`n'size1_y = _se[1b.size#c.diff_ln_IND]
				local se_Ob`n'size2_y = _se[2.size#c.diff_ln_IND]
				cap local OI_b`n'size1_y = _b[1b.size#c.diff_wIPT_IND_CHN]
				cap local OI_b`n'size2_y = _b[2.size#c.diff_wIPT_IND_CHN]
				cap local se_OI_b`n'size1_y = _se[1b.size#c.diff_wIPT_IND_CHN]
				cap local se_OI_b`n'size2_y = _se[2.size#c.diff_wIPT_IND_CHN]
				local ON`n'_y = e(N)
			
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(year nic2)  cluster(nic_08_4dig)
				local Ob`n'size1_yn = _b[1b.size#c.diff_ln_IND]
				local Ob`n'size2_yn = _b[2.size#c.diff_ln_IND]
				local se_Ob`n'size1_yn = _se[1b.size#c.diff_ln_IND]
				local se_Ob`n'size2_yn = _se[2.size#c.diff_ln_IND]
				cap local OI_b`n'size1_yn = _b[1b.size#c.diff_wIPT_IND_CHN]
				cap local OI_b`n'size2_yn = _b[2.size#c.diff_wIPT_IND_CHN]
				cap local se_OI_b`n'size1_yn = _se[1b.size#c.diff_wIPT_IND_CHN]
				cap local se_OI_b`n'size2_yn = _se[2.size#c.diff_wIPT_IND_CHN]
				local ON`n'_yn = e(N)
		
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(nic2#year)  cluster(nic_08_4dig)
				local Ob`n'size1_ycn = _b[1b.size#c.diff_ln_IND]
				local Ob`n'size2_ycn = _b[2.size#c.diff_ln_IND]
				local se_Ob`n'size1_ycn = _se[1b.size#c.diff_ln_IND]
				local se_Ob`n'size2_ycn = _se[2.size#c.diff_ln_IND]
				cap local OI_b`n'size1_ycn = _b[1b.size#c.diff_wIPT_IND_CHN]
				cap local OI_b`n'size2_ycn = _b[2.size#c.diff_wIPT_IND_CHN]
				cap local se_OI_b`n'size1_ycn = _se[1b.size#c.diff_wIPT_IND_CHN]
				cap local se_OI_b`n'size2_ycn = _se[2.size#c.diff_wIPT_IND_CHN]
				local ON`n'_ycn = e(N)
			
* Reduced Form		
	reghdfe `var' $ivs1 $ivs2 $ctrl , a(year)  cluster(nic_08_4dig)
				local Rb`n'size1_y = _b[1b.size#c.diff_ln_IVS]
				local Rb`n'size2_y = _b[2.size#c.diff_ln_IVS]
				local se_Rb`n'size1_y = _se[1b.size#c.diff_ln_IVS]
				local se_Rb`n'size2_y = _se[2.size#c.diff_ln_IVS]
				cap local RI_b`n'size1_y = _b[1b.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local RI_b`n'size2_y = _b[2.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b`n'size1_y = _se[1b.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b`n'size2_y = _se[2.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_y = e(N)
	
	reghdfe `var' $ivs1 $ivs2 $ctrl , a(year nic2)  cluster(nic_08_4dig)
				local Rb`n'size1_yn = _b[1b.size#c.diff_ln_IVS]
				local Rb`n'size2_yn = _b[2.size#c.diff_ln_IVS]
				local se_Rb`n'size1_yn = _se[1b.size#c.diff_ln_IVS]
				local se_Rb`n'size2_yn = _se[2.size#c.diff_ln_IVS]
				cap local RI_b`n'size1_yn = _b[1b.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local RI_b`n'size2_yn = _b[2.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b`n'size1_yn = _se[1b.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b`n'size2_yn = _se[2.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_yn = e(N)

	reghdfe `var' $ivs1 $ivs2 $ctrl , a(nic2#year)  cluster(nic_08_4dig)
				local Rb`n'size1_ycn = _b[1b.size#c.diff_ln_IVS]
				local Rb`n'size2_ycn = _b[2.size#c.diff_ln_IVS]
				local se_Rb`n'size1_ycn = _se[1b.size#c.diff_ln_IVS]
				local se_Rb`n'size2_ycn = _se[2.size#c.diff_ln_IVS]
				cap local RI_b`n'size1_ycn = _b[1b.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local RI_b`n'size2_ycn = _b[2.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b`n'size1_ycn = _se[1b.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b`n'size2_ycn = _se[2.size#c.diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_ycn = e(N)

***
clear
set obs 1
gen bs = 0

gen `var'_b`n'size1_y = `b`n'size1_y'
gen `var'_b`n'size2_y = `b`n'size2_y'
cap gen `var'_I_b`n'size1_y = `I_b`n'size1_y'
cap gen `var'_I_b`n'size2_y = `I_b`n'size2_y'
gen `var'_N`n'_y = `N`n'_y'
gen `var'_F_b`n'size1_y = `F_b`n'size1_y'
gen `var'_F_b`n'size2_y = `F_b`n'size2_y'
cap gen `var'_F_I_b`n'size1_y = `F_I_b`n'size1_y'
cap gen `var'_F_I_b`n'size2_y = `F_I_b`n'size2_y'

gen `var'_b`n'size1_yn = `b`n'size1_yn'
gen `var'_b`n'size2_yn = `b`n'size2_yn'
cap gen `var'_I_b`n'size1_yn = `I_b`n'size1_yn'
cap gen `var'_I_b`n'size2_yn = `I_b`n'size2_yn'
gen `var'_N`n'_yn = `N`n'_yn'
gen `var'_F_b`n'size1_yn = `F_b`n'size1_yn'
gen `var'_F_b`n'size2_yn = `F_b`n'size2_yn'
cap gen `var'_F_I_b`n'size1_yn = `F_I_b`n'size1_yn'
cap gen `var'_F_I_b`n'size2_yn = `F_I_b`n'size2_yn'

gen `var'_b`n'size1_ycn = `b`n'size1_ycn'
gen `var'_b`n'size2_ycn = `b`n'size2_ycn'
cap gen `var'_I_b`n'size1_ycn = `I_b`n'size1_ycn'
cap gen `var'_I_b`n'size2_ycn = `I_b`n'size2_ycn'
gen `var'_N`n'_ycn = `N`n'_ycn'
gen `var'_F_b`n'size1_ycn = `F_b`n'size1_ycn'
gen `var'_F_b`n'size2_ycn = `F_b`n'size2_ycn'
cap gen `var'_F_I_b`n'size1_ycn = `F_I_b`n'size1_ycn'
cap gen `var'_F_I_b`n'size2_ycn = `F_I_b`n'size2_ycn'

gen `var'_Ob`n'size1_y = `Ob`n'size1_y'
gen `var'_Ob`n'size2_y = `Ob`n'size2_y'
cap gen `var'_OI_b`n'size1_y = `OI_b`n'size1_y'
cap gen `var'_OI_b`n'size2_y = `OI_b`n'size2_y'
gen `var'_ON`n'_y = `ON`n'_y'

gen `var'_Ob`n'size1_yn = `Ob`n'size1_yn'
gen `var'_Ob`n'size2_yn = `Ob`n'size2_yn'
cap gen `var'_OI_b`n'size1_yn = `OI_b`n'size1_yn'
cap gen `var'_OI_b`n'size2_yn = `OI_b`n'size2_yn'
gen `var'_ON`n'_yn = `ON`n'_yn'

gen `var'_Ob`n'size1_ycn = `Ob`n'size1_ycn'
gen `var'_Ob`n'size2_ycn = `Ob`n'size2_ycn'
cap gen `var'_OI_b`n'size1_ycn = `OI_b`n'size1_ycn'
cap gen `var'_OI_b`n'size2_ycn = `OI_b`n'size2_ycn'
gen `var'_ON`n'_ycn = `ON`n'_ycn'

gen `var'_Rb`n'size1_y = `Rb`n'size1_y'
gen `var'_Rb`n'size2_y = `Rb`n'size2_y'
cap gen `var'_RI_b`n'size1_y = `I_Rb`n'size1_y'
cap gen `var'_RI_b`n'size2_y = `I_Rb`n'size2_y'
gen `var'_RN`n'_y = `RN`n'_y'

gen `var'_Rb`n'size1_yn = `Rb`n'size1_yn'
gen `var'_Rb`n'size2_yn = `Rb`n'size2_yn'
cap gen `var'_RI_b`n'size1_yn = `I_Rb`n'size1_yn'
cap gen `var'_RI_b`n'size2_yn = `I_Rb`n'size2_yn'
gen `var'_RN`n'_yn = `RN`n'_yn'

gen `var'_Rb`n'size1_ycn = `Rb`n'size1_ycn'
gen `var'_Rb`n'size2_ycn = `Rb`n'size2_ycn'
cap gen `var'_RI_b`n'size1_ycn = `I_Rb`n'size1_ycn'
cap gen `var'_RI_b`n'size2_ycn = `I_Rb`n'size2_ycn'
gen `var'_RN`n'_ycn = `RN`n'_ycn'

save "$tempfold/bs`n'_`dataspec'_`var'", replace

*** SE
clear
set obs 1
gen bs = -1

gen `var'_b`n'size1_y = `se_b`n'size1_y'
gen `var'_b`n'size2_y = `se_b`n'size2_y'
cap gen `var'_I_b`n'size1_y = `se_I_b`n'size1_y'
cap gen `var'_I_b`n'size2_y = `se_I_b`n'size2_y'

gen `var'_b`n'size1_yn = `se_b`n'size1_yn'
gen `var'_b`n'size2_yn = `se_b`n'size2_yn'
cap gen `var'_I_b`n'size1_yn = `se_I_b`n'size1_yn'
cap gen `var'_I_b`n'size2_yn = `se_I_b`n'size2_yn'

gen `var'_b`n'size1_ycn = `se_b`n'size1_ycn'
gen `var'_b`n'size2_ycn = `se_b`n'size2_ycn'
cap gen `var'_I_b`n'size1_ycn = `se_I_b`n'size1_ycn'
cap gen `var'_I_b`n'size2_ycn = `se_I_b`n'size2_ycn'

gen `var'_Ob`n'size1_y = `se_Ob`n'size1_y'
gen `var'_Ob`n'size2_y = `se_Ob`n'size2_y'
cap gen `var'_OI_b`n'size1_y = `se_OI_b`n'size1_y'
cap gen `var'_OI_b`n'size2_y = `se_OI_b`n'size2_y'

gen `var'_Ob`n'size1_yn = `se_Ob`n'size1_yn'
gen `var'_Ob`n'size2_yn = `se_Ob`n'size2_yn'
cap gen `var'_OI_b`n'size1_yn = `se_OI_b`n'size1_yn'
cap gen `var'_OI_b`n'size2_yn = `se_OI_b`n'size2_yn'

gen `var'_Ob`n'size1_ycn = `se_Ob`n'size1_ycn'
gen `var'_Ob`n'size2_ycn = `se_Ob`n'size2_ycn'
cap gen `var'_OI_b`n'size1_ycn = `se_OI_b`n'size1_ycn'
cap gen `var'_OI_b`n'size2_ycn = `se_OI_b`n'size2_ycn'

gen `var'_Rb`n'size1_y = `se_Rb`n'size1_y'
gen `var'_Rb`n'size2_y = `se_Rb`n'size2_y'
cap gen `var'_RI_b`n'size1_y = `se_I_Rb`n'size1_y'
cap gen `var'_RI_b`n'size2_y = `se_I_Rb`n'size2_y'

gen `var'_Rb`n'size1_yn = `se_Rb`n'size1_yn'
gen `var'_Rb`n'size2_yn = `se_Rb`n'size2_yn'
cap gen `var'_RI_b`n'size1_yn = `se_I_Rb`n'size1_yn'
cap gen `var'_RI_b`n'size2_yn = `se_I_Rb`n'size2_yn'

gen `var'_Rb`n'size1_ycn = `se_Rb`n'size1_ycn'
gen `var'_Rb`n'size2_ycn = `se_Rb`n'size2_ycn'
cap gen `var'_RI_b`n'size1_ycn = `se_I_Rb`n'size1_ycn'
cap gen `var'_RI_b`n'size2_ycn = `se_I_Rb`n'size2_ycn'

save "$tempfold/bs`n'_`dataspec'_`var'_se", replace
restore
}

*** Compile results:
use "$tempfold/bs`n'_`dataspec'_diff_tfpr_se", clear
foreach var in dif_d_bar dif_a_bar dif_cov_stay dif_var_adj adding_term drop_term {
	merge 1:1 bs using "$tempfold/bs`n'_`dataspec'_`var'_se", assert(3) nogen
	}
save "$tempfold/bs`n'_`dataspec'_se", replace

use "$tempfold/bs`n'_`dataspec'_diff_tfpr", clear
foreach var in dif_d_bar dif_a_bar dif_cov_stay dif_var_adj adding_term drop_term {
	merge 1:1 bs using "$tempfold/bs`n'_`dataspec'_`var'", assert(3) nogen
	}
append using "$tempfold/bs`n'_`dataspec'_se"
order bs
save "$tempfold/bs`n'_`dataspec'", replace

